package com.stylefeng.guns.modular.analysis.manager;

import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.persistence.model.*;
import com.stylefeng.guns.modular.train.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StreamUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by jondai on 2018/1/21.
 * 数据统计分析服务类
 */
@Service
public class AnalysisManager {

    @Autowired
    private INetworkTrainPointsService networkTrainPointsService;
    @Autowired
    private IFulltimeTrainPointsService fulltimeTrainPointsService;
    @Autowired
    private IProtechTrainPointsService protechTrainPointsService;
    @Autowired
    private IContestPointsService contestPointsService;
    @Autowired
    private ICycleAuthenPointsService cycleAuthenPointsService;
    @Autowired
    private IDegreeTrainPointsService degreeTrainPointsService;
    @Autowired
    private IExpertTalentsPointsService expertTalentsPointsService;
    @Autowired
    private IProjectAuthenPointsService projectAuthenPointsService;
    @Autowired
    private IProqualifyTrainPointsService proqualifyTrainPointsService;
    @Autowired
    private ITrainerAuthenPointsService trainerAuthenPointsService;
    @Autowired
    private ITrainerExcellentPointsService trainerExcellentPointsService;
    @Autowired
    private IMentoringRelationPointsService mentoringRelationPointsService;

    @Autowired
    private IPracticePointsService practicePointsService;
    @Autowired
    private ITrainProgramPointsService trainProgramPointsService;

    public Map<String,Object> getBaseTrain(String empCode, String year){
        Map<String,Object> baseMap = new HashMap<>();

        /**
         * 基础培训记录
         */
        Map<String,Object> condition = new HashMap<>();
        condition.put("emp_code",empCode);
        if(!StringUtils.isEmpty(year)){
            condition.put("year",year);
        }

        List<NetworkTrainPoints> networkTrainPointss = networkTrainPointsService.selectByMap(condition);
        Double net_hours = 0.0;
        Double net_integral = 0.0;
        for (NetworkTrainPoints networkTrainPoints : networkTrainPointss) {
            net_hours += networkTrainPoints.getAnnualPeriod();
            net_integral += networkTrainPoints.getAnnualCredits();
        }
        baseMap.put("net_hours",net_hours);
        baseMap.put("net_point",net_integral);


        List<FulltimeTrainPoints> fulltimeTrainPointss = fulltimeTrainPointsService.selectByMap(condition);
        Double ftp_point = 0.0;
        for (FulltimeTrainPoints fulltimeTrainPoints : fulltimeTrainPointss) {
            ftp_point += fulltimeTrainPoints.getCredit();
        }
        baseMap.put("ftps",fulltimeTrainPointss);
        baseMap.put("ftp_point",ftp_point);
        baseMap.put("base_point",ftp_point + net_integral);


        Double other_point = 0.0;
        /**
         * 个人素质
         */
        //学历、学位
        List<DegreeTrainPoints> degreeTrainPointss = degreeTrainPointsService.selectByMap(condition);
        DegreeTrainPoints degreeTrain;
        if(degreeTrainPointss != null && degreeTrainPointss.size() > 0){
            degreeTrain = degreeTrainPointss.get(0);
            other_point += degreeTrain.getIntegral();
        }else{
            degreeTrain = new DegreeTrainPoints();
            degreeTrain.setIntegral(0.0);
        }
        baseMap.put("dtp",degreeTrain);
        //专业技术资格
        List<ProtechTrainPoints> protechTrainPointss = protechTrainPointsService.selectByMap(condition);
        ProtechTrainPoints protechTrainPoint;
        if(protechTrainPointss != null && protechTrainPointss.size() > 0){
            protechTrainPoint = protechTrainPointss.get(0);
            other_point += protechTrainPoint.getIntegral();
        }else{
            protechTrainPoint = new ProtechTrainPoints();
            protechTrainPoint.setIntegral(0.0);
        }
        baseMap.put("pttp",protechTrainPoint);

        //职业资格
        List<ProqualifyTrainPoints> proqualifyTrainPointList = proqualifyTrainPointsService.selectByMap(condition);
        ProqualifyTrainPoints proqualifyTrain;
        if(proqualifyTrainPointList != null && proqualifyTrainPointList.size() > 0){
            proqualifyTrain = proqualifyTrainPointList.get(0);
            other_point += proqualifyTrain.getIntegral();
        }else{
            proqualifyTrain = new ProqualifyTrainPoints();
            proqualifyTrain.setIntegral(0.0);
        }
        baseMap.put("pqtp",proqualifyTrain);

        //专家人才
        List<ExpertTalentsPoints> expertTalentsPointsList = expertTalentsPointsService.selectByMap(condition);
        ExpertTalentsPoints expertTalents;
        if(expertTalentsPointsList != null && expertTalentsPointsList.size() > 0){
            expertTalents = expertTalentsPointsList.get(0);
            other_point += expertTalents.getIntegral();
        }else{
            expertTalents = new ExpertTalentsPoints();
            expertTalents.setIntegral(0.0);
        }
        baseMap.put("etp",expertTalents);

        //竞赛调考
        List<ContestPoints> contestPointsList = contestPointsService.selectByMap(condition);
        ContestPoints contestPoint;
        if(contestPointsList != null && contestPointsList.size() > 0){
            contestPoint = contestPointsList.get(0);

            String temp = contestPoint.getCompanyType() + "-" + contestPoint.getContestType() + "-" + contestPoint.getWinType();
            contestPoint.setCompanyType(temp);
            other_point += contestPoint.getIntegral();
        }else{
            contestPoint = new ContestPoints();
            contestPoint.setIntegral(0.0);
        }
        baseMap.put("ctp",contestPoint);

        //执业资格
        List<PracticePoints> practicePointsList = practicePointsService.selectByMap(condition);
        PracticePoints practicePoint;
        if(practicePointsList != null && practicePointsList.size() > 0){
            practicePoint = practicePointsList.get(0);
            other_point += practicePoint.getIntegral();
        }else{
            practicePoint = new PracticePoints();
            practicePoint.setIntegral(0.0);
        }
        baseMap.put("pp",practicePoint);
        /**
         * 培训能力
         */

        //培训项目开发
        List<TrainProgramPoints> trainProgramPointsList = trainProgramPointsService.selectByMap(condition);
        TrainProgramPoints trainProgramPoint;
        if(trainProgramPointsList != null && trainProgramPointsList.size() > 0){
            trainProgramPoint = trainProgramPointsList.get(0);
            other_point += trainProgramPoint.getIntegral();
        }else{
            trainProgramPoint = new TrainProgramPoints();
            trainProgramPoint.setIntegral(0.0);
        }
        baseMap.put("tpp",trainProgramPoint);

        /**
         * 兼职培训师
         */
        //认证兼职培训师
        List<TrainerAuthenPoints> trainerAuthenPointsList = trainerAuthenPointsService.selectByMap(condition);
        TrainerAuthenPoints trainerAuthenPoint;
        if(trainerAuthenPointsList != null && trainerAuthenPointsList.size() > 0){
            trainerAuthenPoint = trainerAuthenPointsList.get(0);
            Double point = 0.0;
            //如果有多条，将积分合并
            for (TrainerAuthenPoints trainerAuthenPoints : trainerAuthenPointsList) {
                point +=trainerAuthenPoints.getIntegral();
            }

            trainerAuthenPoint.setIntegral(point);
            other_point += trainerAuthenPoint.getIntegral();
        }else{
            trainerAuthenPoint = new TrainerAuthenPoints();
            trainerAuthenPoint.setIntegral(0.0);
        }
        String authen = trainerAuthenPoint.getAuthen() == null ? "" : trainerAuthenPoint.getAuthen()+"认证兼职培训师";
        trainerAuthenPoint.setDataOrigin(authen);
        baseMap.put("tap",trainerAuthenPoint);

        //优秀兼职培训师
        List<TrainerExcellentPoints> trainerExcellentPointsList = trainerExcellentPointsService.selectByMap(condition);

        TrainerExcellentPoints trainerExcellentPoint;
        if(trainerExcellentPointsList != null && trainerExcellentPointsList.size() > 0){
            trainerExcellentPoint = trainerExcellentPointsList.get(0);
            Double point = 0.0;
            //如果有多条，将积分合并
            for (TrainerExcellentPoints tr : trainerExcellentPointsList) {
                point +=tr.getIntegral();
            }

            trainerExcellentPoint.setIntegral(point);
            other_point += trainerExcellentPoint.getIntegral();
        }else{
            trainerExcellentPoint = new TrainerExcellentPoints();
            trainerExcellentPoint.setIntegral(0.0);
        }
        String temp = trainerExcellentPoint.getAuthen() == null ? "" : trainerExcellentPoint.getAuthen()+"优秀兼职培训师";
        trainerExcellentPoint.setDataOrigin(temp);
        baseMap.put("tep",trainerExcellentPoint);

        //项目兼职培训师
        List<ProjectAuthenPoints> projectAuthenPointsList = projectAuthenPointsService.selectByMap(condition);

        ProjectAuthenPoints projectAuthenPoint;
        if(projectAuthenPointsList != null && projectAuthenPointsList.size() > 0){
            projectAuthenPoint = projectAuthenPointsList.get(0);
            Double point = 0.0;
            //如果有多条，将积分合并
            for (ProjectAuthenPoints tr : projectAuthenPointsList) {
                point +=tr.getIntegral();
            }

            projectAuthenPoint.setIntegral(point);
            other_point += projectAuthenPoint.getIntegral();
        }else{
            projectAuthenPoint = new ProjectAuthenPoints();
            projectAuthenPoint.setIntegral(0.0);
        }
        String temp3 = projectAuthenPoint.getTeachLevel() == null ? "" : projectAuthenPoint.getTeachLevel()+ "项目制兼职培训师";
        projectAuthenPoint.setDataOrigin( temp3 );
        baseMap.put("pap",projectAuthenPoint);

        //周期制兼职培训师
        List<CycleAuthenPoints> cycleAuthenPointsList = cycleAuthenPointsService.selectByMap(condition);

        CycleAuthenPoints cycleAuthenPoint;
        if(cycleAuthenPointsList != null && cycleAuthenPointsList.size() > 0){
            cycleAuthenPoint = cycleAuthenPointsList.get(0);
            Double point = 0.0;
            //如果有多条，将积分合并
            for (CycleAuthenPoints tr : cycleAuthenPointsList) {
                point +=tr.getIntegral();
            }

            cycleAuthenPoint.setIntegral(point);
            other_point += cycleAuthenPoint.getIntegral();


        }else{
            cycleAuthenPoint = new CycleAuthenPoints();
            cycleAuthenPoint.setIntegral(0.0);
        }

        //是否合格
        if("合格".equals(cycleAuthenPoint.getJudgeResult())){
            cycleAuthenPoint.setDataOrigin("周期制兼职培训师");
        }else{
            cycleAuthenPoint = new CycleAuthenPoints();
            cycleAuthenPoint.setIntegral(0.0);
        }

        baseMap.put("cap",cycleAuthenPoint);

        //师带徒
        List<MentoringRelationPoints> mentoringRelationPointsList = mentoringRelationPointsService.selectByMap(condition);

        MentoringRelationPoints master = new MentoringRelationPoints();
        MentoringRelationPoints student = new MentoringRelationPoints();
        master.setIntegral(0.0);
        student.setIntegral(0.0);
        student.setDataOrigin("徒弟");
        master.setDataOrigin("师父");

        if(mentoringRelationPointsList != null && mentoringRelationPointsList.size() > 0){
            for (MentoringRelationPoints mentoringRelationPoints : mentoringRelationPointsList) {
                //徒弟积分
                if(mentoringRelationPoints.getIntegralTypeCode().equals(Const.MENTORING_INTEGRAL_TYPE_APPRENTICE_CODE)){

//                    Double stPoint = student.getIntegral() == null ? 0.0 : student.getIntegral();
//                    Double mePoint = mentoringRelationPoints.getIntegral() == null ? 0.0 : mentoringRelationPoints.getIntegral();
//                    student.setIntegral(stPoint + mePoint);
                    student = mentoringRelationPoints;
                }else{
                    master = mentoringRelationPoints;
//                    Double maPoint = master.getIntegral() == null ? 0.0 : master.getIntegral();
//                    Double mePoint = mentoringRelationPoints.getIntegral() == null ? 0.0 : mentoringRelationPoints.getIntegral();
//                    student.setIntegral(maPoint + mePoint);
                }
            }


        }
        other_point += student.getIntegral();
        other_point += master.getIntegral();
        baseMap.put("master",master);
        baseMap.put("student",student);

        baseMap.put("other_point",other_point);
        return baseMap;
    }

    /**
     * 统计个人素质积分： 学历提升|专业技术提升| 职业|人才积分
     * @param empCodes
     * @return
     */
    public Double totalQualityPoints(List<String> empCodes, String year){
        Double total = 0.0;
        //学历
        total += totalDegreeTrainPoints(empCodes,year);
        //专业
        total += totalProTechPoints(empCodes,year);
        //职业
        total += totalProqualifyTrainPoints(empCodes,year);
        //人才积分
        total += totalExpertTalentsPoints(empCodes,year);
        return total;
    }

    /**
     * 统计网络培训积分
     * @return
     */
    public Double totalNetWorkPoints(List<String> empCodes, String year){
        Double total = networkTrainPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 统计脱产培训积分
     * @param empCodes
     * @return
     */
    public Double totalFullTimePoints(List<String> empCodes, String year){
        Double total = fulltimeTrainPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }


    /**
     * 学历提升积分
     * @param empCodes
     * @return
     */
    public Double totalDegreeTrainPoints(List<String> empCodes, String year){
        Double total = degreeTrainPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 专业技术资格积分
     * @param empCodes
     * @return
     */
    public Double totalProTechPoints(List<String> empCodes, String year){
        Double total = protechTrainPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 职业资格积分
     * @param empCodes
     * @return
     */
    public Double totalProqualifyTrainPoints(List<String> empCodes, String year){
        Double total = proqualifyTrainPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }
    /**
     * 执业资格积分
     * @param empCodes
     * @return
     */
    public Double totalPracticePointsPoints(List<String> empCodes, String year){
        Double total = practicePointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }
    /**
     * 竞赛调考积分
     * @param empCodes
     * @return
     */
    public Double totalContestPoints(List<String> empCodes, String year){
        Double total = contestPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }




    /**
     * 认证兼职培训师
     * @param empCodes
     * @return
     */
    public Double totalTrainerAuthenPonints(List<String> empCodes, String year){
        Double total = trainerAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }



    /**
     * 优秀兼职培训师
     * @param empCodes
     * @return
     */
    public Double totalTrainerExcellentPoints(List<String> empCodes, String year){
        Double total = trainerExcellentPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }


    /**
     * 项目兼职培训师
     * @param empCodes
     * @return
     */
    public Double totalProjectAuthenPoints(List<String> empCodes, String year){
        Double total = projectAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 周期制兼职培训师积分
     * @param empCodes
     * @return
     */
    public Double totalCycleAuthenPoints(List<String> empCodes, String year){
        Double total = cycleAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 专家人才培训积分
     * @param empCodes
     * @return
     */
    public Double totalExpertTalentsPoints(List<String> empCodes, String year){
        Double total = expertTalentsPointsService.sumIntegralInEmpCode(empCodes,year);
        return total == null ? 0.0 :total;
    }

    /**
     * 培训能力积分
     * @param empCodes
     * @param year
     * @return
     */
    public Double totalTrainAbility(List<String> empCodes, String year){
        if(CollectionUtils.isEmpty(empCodes) && StringUtils.isEmpty(year)){
            return 0.0;
        }
        Double renzheng = trainerAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        Double youxiu = trainerExcellentPointsService.sumIntegralInEmpCode(empCodes,year);
        Double xiangmu = projectAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        Double zhouqi = cycleAuthenPointsService.sumIntegralInEmpCode(empCodes,year);
        Double shitu =  mentoringRelationPointsService.sumIntegralInEmpCode(empCodes,year);
        Double project =  trainProgramPointsService.sumIntegralInEmpCode(empCodes,year);
        renzheng = renzheng == null ? 0.0 : renzheng;
        youxiu = youxiu == null ? 0.0 : youxiu;
        xiangmu = xiangmu == null ? 0.0 : xiangmu;
        zhouqi = zhouqi == null ? 0.0 : zhouqi;
        shitu = shitu == null ? 0.0 : shitu;
        project = project == null ? 0.0 : project;

        Double total = 0.0;
        total = renzheng + youxiu + xiangmu + zhouqi + shitu + project;
        return total;
    }

}
